package org.beiji.frameworks.convention.exception;


import lombok.Getter;
import org.beiji.frameworks.convention.errorcode.IErrorCode;
import org.springframework.util.StringUtils;

import java.util.Optional;

@Getter
public abstract class AbstractException extends RuntimeException {


    public final String errorCode;

    public final String errorMessage;

    public AbstractException(String message, Throwable cause, IErrorCode errorCode) {
        // super的精妙之处就是cause，可以不丢失异常错误的堆栈信息
        super(message, cause);
        this.errorCode = errorCode.code();
        this.errorMessage = Optional.ofNullable(StringUtils.hasLength(message) ? message :  null).orElse(errorCode.message());
    }
}
